package org.ptkparty.client.view.scales;

import org.ptkparty.client.activity.ScalesActivity;
import org.ptkparty.client.model.ScaleProxy;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.IsEditor;
import com.google.gwt.editor.client.adapters.HasDataEditor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SingleSelectionModel;

public class ScalesView extends Composite implements IsEditor<HasDataEditor<ScaleProxy>> {

	private static ScalesViewUiBinder uiBinder = GWT.create(ScalesViewUiBinder.class);

	interface ScalesViewUiBinder extends UiBinder<Widget, ScalesView> {
	}

	private ScalesActivity activity;
	private HasDataEditor<ScaleProxy> editor;
	private ListDataProvider<ScaleProxy> provider;
	private SingleSelectionModel<ScaleProxy> selectionModel;

	@UiField(provided = true)
	DataGrid<ScaleProxy> scaleTable;

	@UiField
	Button addBtn;

	@UiHandler("addBtn")
	public void onAddBtnClicked(ClickEvent event) {
		editor.getList().add(activity.addScale());
		provider.setList(editor.getList());
	}

	@UiField
	Button saveBtn;

	@UiHandler("saveBtn")
	public void onSaveBtnClicked(ClickEvent clickEvent) {
		activity.save();
	}

	@UiField
	Button deleteBtn;

	@UiHandler("deleteBtn")
	public void onDeleteBtnClicked(ClickEvent event) {
		activity.delete(selectionModel.getSelectedObject());
	}

	public ScalesView() {
		scaleTable = new DataGrid<ScaleProxy>();

		SingleSelectionModel<ScaleProxy> selectionModel = new SingleSelectionModel<ScaleProxy>();
		scaleTable.setSelectionModel(selectionModel);

		provider = new ListDataProvider<ScaleProxy>();
		provider.addDataDisplay(scaleTable);

		editor = HasDataEditor.of(scaleTable);

		Column<ScaleProxy, String> minColumn = new Column<ScaleProxy, String>(new EditTextCell()) {

			@Override
			public String getValue(ScaleProxy object) {
				return object.getMin() == null ? "" : object.getMin().toString();
			}

		};
		minColumn.setDataStoreName("min");
		minColumn.setFieldUpdater(new FieldUpdater<ScaleProxy, String>() {

			@Override
			public void update(int index, ScaleProxy object, String value) {
				if (value != null && !value.isEmpty()) {
					object.setMin(Double.valueOf(value));
				}
			}
		});
		scaleTable.addColumn(minColumn, "Min");

		Column<ScaleProxy, String> maxColumn = new Column<ScaleProxy, String>(new EditTextCell()) {

			@Override
			public String getValue(ScaleProxy object) {
				return object.getMax() == null ? "" : object.getMax().toString();
			}

		};
		maxColumn.setDataStoreName("max");
		maxColumn.setFieldUpdater(new FieldUpdater<ScaleProxy, String>() {

			@Override
			public void update(int index, ScaleProxy object, String value) {
				if (value != null && !value.isEmpty()) {
					object.setMax(Double.valueOf(value));
				}
			}
		});
		scaleTable.addColumn(maxColumn, "Max");

		Column<ScaleProxy, String> normalWinColumn = new Column<ScaleProxy, String>(new EditTextCell()) {

			@Override
			public String getValue(ScaleProxy object) {
				return object.getNormalWin() == null ? "" : object.getNormalWin().toString();
			}

		};
		normalWinColumn.setDataStoreName("normalWin");
		normalWinColumn.setFieldUpdater(new FieldUpdater<ScaleProxy, String>() {

			@Override
			public void update(int index, ScaleProxy object, String value) {
				if (value != null && !value.isEmpty()) {
					object.setNormalWin(Double.valueOf(value));
				}
			}
		});
		scaleTable.addColumn(normalWinColumn, "Victoire normale");

		Column<ScaleProxy, String> normalLooseColumn = new Column<ScaleProxy, String>(new EditTextCell()) {

			@Override
			public String getValue(ScaleProxy object) {
				return object.getNormalLoose() == null ? "" : object.getNormalLoose().toString();
			}

		};
		normalLooseColumn.setDataStoreName("normalLoose");
		normalLooseColumn.setFieldUpdater(new FieldUpdater<ScaleProxy, String>() {

			@Override
			public void update(int index, ScaleProxy object, String value) {
				if (value != null && !value.isEmpty()) {
					object.setNormalLoose(Double.valueOf(value));
				}
			}
		});
		scaleTable.addColumn(normalLooseColumn, "Defaite normale");

		Column<ScaleProxy, String> anormalWinColumn = new Column<ScaleProxy, String>(new EditTextCell()) {

			@Override
			public String getValue(ScaleProxy object) {
				return object.getAnormalWin() == null ? "" : object.getAnormalWin().toString();
			}

		};
		anormalWinColumn.setDataStoreName("anormalWin");
		anormalWinColumn.setFieldUpdater(new FieldUpdater<ScaleProxy, String>() {

			@Override
			public void update(int index, ScaleProxy object, String value) {
				if (value != null && !value.isEmpty()) {
					object.setAnormalWin(Double.valueOf(value));
				}
			}
		});
		scaleTable.addColumn(anormalWinColumn, "Victoire anormale");

		Column<ScaleProxy, String> anormalLooseColumn = new Column<ScaleProxy, String>(new EditTextCell()) {

			@Override
			public String getValue(ScaleProxy object) {
				return object.getAnormalLoose() == null ? "" : object.getAnormalLoose().toString();
			}

		};
		anormalLooseColumn.setDataStoreName("anormalLoose");
		anormalLooseColumn.setFieldUpdater(new FieldUpdater<ScaleProxy, String>() {

			@Override
			public void update(int index, ScaleProxy object, String value) {
				if (value != null && !value.isEmpty()) {
					object.setAnormalLoose(Double.valueOf(value));
				}
			}
		});
		scaleTable.addColumn(anormalLooseColumn, "Defaite anormale");

		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setPresenter(ScalesActivity scalesActivity) {
		activity = scalesActivity;
	}

	@Override
	public HasDataEditor<ScaleProxy> asEditor() {
		return editor;
	}

	public void removeUserEditor(ScaleProxy selectedObject) {
		editor.getList().remove(selectedObject);
		provider.refresh();
	}

	public void readOnlyMode() {
		saveBtn.setVisible(false);
		addBtn.setVisible(false);
		deleteBtn.setVisible(false);
	}

}
